package com.btpj.blog_server.controller;

import com.btpj.blog_server.entity.BaseResp;
import com.btpj.blog_server.entity.User;
import com.btpj.blog_server.service.UserService;
import com.btpj.blog_server.utils.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/getCurrentUser")
    public BaseResp<User> getCurrentUser() {
        return new BaseResp<>(0, "请求成功", UserManager.getCurrentUser());
    }

    @GetMapping("/isAdmin")
    public BaseResp<Boolean> isAdmin() {
        return new BaseResp<>(0, "请求成功", UserManager.isAdmin());
    }

    @GetMapping("/getUserEmail")
    public BaseResp<String> getUserEmail() {
        return new BaseResp<>(0, "请求成功", UserManager.getCurrentUser().getEmail());
    }

    @GetMapping("/updateUserEmail")
    public BaseResp<Void> updateUserEmail(String email) {
        if (userService.updateUserEmail(UserManager.getCurrentUser().getId(), email) == 1) {
            return new BaseResp<>(0, "配置邮箱成功");
        } else {
            return new BaseResp<>(1, "配置邮箱失败");
        }
    }
}
